package com.sptus.web.quartz.job;

import cn.hutool.core.util.ObjectUtil;
import com.sptus.web.service.CloudIEquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Service
@Slf4j
public class SptHomeHealth  {
    @Autowired
    CloudIEquipmentService equipmentService;

    //首页健康状态，数量，设备类型更新
    @Scheduled(cron = "0 0/30 * * * ?")
    public void execute() throws JobExecutionException {
        ArrayList<HashMap<String, Object>> equipmentGenration = equipmentService.getEquipmentGenration();
        Map<String, ArrayList<HashMap<String, Object>>> groupedMap = new HashMap<>();

        for (HashMap<String, Object> map : equipmentGenration) {
            String key = map.get("battery_number").toString();
            if(key.equals("0")||key.equals("1")||key.equals("2")){
                key="2";
            }
            if (!groupedMap.containsKey(key)) {
                groupedMap.put(key, new ArrayList<>());
            }
            groupedMap.get(key).add(map);
        }
        Set<String> strings = groupedMap.keySet();
        for (String string : strings) {

            ArrayList<HashMap<String, Object>> arrayList = groupedMap.get(string);
            HashMap<String,Object> devBG=new HashMap<>();

            int status=0;
            int Malfunction=0;
            int Alarm=0;
            int Off_Line=0;
            for (HashMap<String, Object> stringObjectHashMap : arrayList) {
                if( stringObjectHashMap.get("status").equals("1")){
                    status+=1;
                    if( stringObjectHashMap.get("status").equals(3)){
                        Malfunction+=1;
                    }
                    if(  stringObjectHashMap.get("status").equals(4)){
                        Alarm+=1;
                    }

                }else  if( stringObjectHashMap.get("status").equals("0")){
                    Off_Line+=1;
                }
            }
            devBG.put("health_num",status);
            devBG.put("malfunction",Malfunction);
            devBG.put("alarm",Alarm);
            devBG.put("off_line",Off_Line);

            if(string.equals("0")){

            }else
            if(string.equals("1")){

            }
            else
            if(string.equals("2")){
                HashMap<String, Object> hashMap=   equipmentService.getHomeHealth( "1");
                devBG.put("devType","1");
               if(!ObjectUtil.isEmpty(hashMap)) {
                   devBG.put("id",hashMap.get("id"));
               }
                equipmentService.saveHomeHealth(devBG);
            }
            else
            if(string.equals("3")){
                HashMap<String, Object> hashMap=   equipmentService.getHomeHealth( "2");
                devBG.put("devType","2");
                if(!ObjectUtil.isEmpty(hashMap)) {

                    devBG.put("id",hashMap.get("id"));
                }
                equipmentService.saveHomeHealth(devBG);
            }
            else
            if(string.equals("4")){
                HashMap<String, Object> hashMap=   equipmentService.getHomeHealth( "3");
                devBG.put("devType","3");
                if(!ObjectUtil.isEmpty(hashMap)) {
                    devBG.put("id",hashMap.get("id"));
                }
                equipmentService.saveHomeHealth(devBG);
            }

        }
    }
}
